package com.bds.tjj.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import com.bds.tjj.model.Messages;
import com.bds.tjj.model.MonthData;
import com.bds.tjj.model.News;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.model.common.Page;
import com.bds.tjj.utils.Const;
import com.bds.tjj.utils.Tools;

/**
 * Created by guoyu on 2018/1/29.
 */
@Service
public class NewsService {
	private org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(NewsService.class);
	@Resource
	private NutDao dao;

	public List<News> getLastedNews(int limit) {

		List<News> ret = new ArrayList<News>();
		String[] images = { "#040a2b", "#040a2b", "#040a2b", "#040a2b", "#040a2b" };

		try {
			ret = dao.query(News.class, Cnd.NEW().limit(1, limit).desc("pubdate"));
		} catch (Exception e) {

			LOGGER.error("查询新闻出错", e);

		}

		for (int i = 0; i < ret.size(); i++) {
			News n = ret.get(i);

			n.setLocal_img(images[i]);
		}

		return ret;
	}

	public List<MonthData> getLastedIndex(int limit) throws ParseException {

		// SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		List<MonthData> arry = new ArrayList<MonthData>();

		String sql = "SELECT DISTINCT(m.id)    FROM T_MONTH_DATA m,T_PROP_INDEX p where   m.data is not null and m.data <> '' and p.prop=m.prop and p.status=1 ";

		Sql sq = Sqls.create(sql);
		sq.setCallback(Sqls.callback.longs());
		dao.execute(sq);
		List<Long> ids = sq.getList(Long.class);
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM");
		for (int i = 0; i < ids.size(); i++) {

			Long id = ids.get((ids.size() - 1) - i);

			MonthData m = dao.fetch(MonthData.class, Cnd.where("id", "=", id));
			
			if (m.getYm() != null) {
				Date yearMonth = sdf1.parse(m.getYm() + "");
				String vym = sdf2.format(yearMonth);
				m.setView_ym(vym);
			}
			PropIndex p = dao.fetch(PropIndex.class, Cnd.where("prop", "=", m.getProp()));
			m.setUnit(p.getUnit());
			if (arry.size() < 3) {
				arry.add(m);
			} else {
				break;
			}

		}

		return arry;
	}

	public List<News> getMessagesNews(Long user_id, int limit) {
		List<News> ret = new ArrayList<News>();
		List<Messages> list = dao.query(Messages.class,
				Cnd.where("user_id", "=", user_id).and("isread", "=", 0).limit(1, limit));
		for (Messages n : list) {
			News news = dao.fetch(News.class, Cnd.where("id", "=", n.getNews_id()));
			int r = Tools.getRandomNum(10);
			news.setLocal_img(r + ".jpg");
			ret.add(news);
		}
		return ret;
	}

	public News upNews(Long user_id, Long newsid) {
		dao.update(Messages.class, Chain.make("isread", 1),
				Cnd.where("user_id", "=", user_id).and("news_id", "=", newsid));
		News news = dao.fetch(News.class, Cnd.where("id", "=", newsid));
		return news;
	}

	private void upNews(List<News> mes, Long user_id) {
		for (News n : mes) {
			dao.update(Messages.class, Chain.make("isread", 1),
					Cnd.where("user_id", "=", user_id).and("news_id", "=", n.getId()));
		}

	}

	public Page getNewsList(Long user_id, Integer pageNum, Integer pageSize, boolean markRead) {
		Pager pager = dao.createPager(pageNum, pageSize);
		Criteria cri = Cnd.cri();

		cri.getOrderBy().desc("pubdate");
		List<News> list = dao.query(News.class, cri, pager);
		for (News n : list) {

			int r = Tools.getRandomNum(13);
			n.setLocal_img(r + ".jpg");

		}
		int count = dao.count(News.class, cri);
		Page p = new Page(count, pager.getPageNumber(), pager.getPageSize());
		p.setCode(Const.SUC);
		p.setData(list);
		if (markRead) {
			this.upNews(list, user_id);
		}

		return p;

	}

	public void saveNews(News news) {
		try {
			News n = dao.fetch(News.class, Cnd.where("link", "=", news.getLink()));
			if (n == null) {
				dao.fastInsert(news);
			}
		} catch (Exception e) {
			LOGGER.error("", e);
		}

	}

}
